package com.njdj.gps.record;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.njdj.gps.common.constants.NjdjConstants;
import com.njdj.gps.kernel.entity.GpsCarHis;


public class QueryCarHistory {
	
	static HttpClient client = new DefaultHttpClient();
	
	private String carid = "";
	private String sTime = "";
	private String eTime = "";
	private List<GpsCarHis> gpsCarHisList = new ArrayList<GpsCarHis>();
	
	public void setCarId(String carId){
		this.carid = carId;
	}
	public void setSTime(String sTime){
		this.sTime = sTime;
	}
	public void setETime(String eTime){
		this.eTime = eTime;
	}
	
	public String getCarId(){
		return this.carid;
	}
	public String getSTime(){
		return this.sTime;
	}
	public String getETime(){
		return this.eTime;
	}
	public List<GpsCarHis> getGpsCarHisList(){
		return this.gpsCarHisList;
	}
	
	public List<GpsCarHis> query(String carid, String username, String password) {
		
		HttpGet getMethod = new HttpGet("http://113.106.91.167:8080/manbucc/");
		
		//登录
		StringBuffer loginUrl = new StringBuffer();
		loginUrl.append("http://113.106.91.167:8080/Action/Login");
		loginUrl.append("?v=2&mapID=0&type=3&name=");
		loginUrl.append(username);
		loginUrl.append("&pw=");
		loginUrl.append(password);
		loginUrl.append("&forexp=0");
		Date now = new Date();
		int offset = now.getTimezoneOffset()/60*(-1);
		loginUrl.append("&timeZone="+offset);
		
		HttpPost postMethod = new HttpPost(loginUrl.toString());
		String loginResult = new String();
		try{
			HttpResponse loginResponse = client.execute(postMethod);
			String loginResponseEntity = EntityUtils.toString(loginResponse.getEntity());
			loginResult = loginResponseEntity;
		} catch (ClientProtocolException e) {
			System.out.println("ClientProtocolException");
		} catch (IOException e) {
			System.out.println("IOException");
		}
		
		//判断登录信息
		if(null != loginResult && loginResult.startsWith("1")){
			//登录成功
			try {
				postMethod = new HttpPost("http://113.106.91.167:8080/Action/History?ram=0.5639728430726747");
				StringEntity reqEntity = new StringEntity("carID="+this.getCarId()+"&sTime="+this.sTime+"&eTime="+this.eTime);
				System.out.println("carID="+this.getCarId()+"&sTime="+this.sTime+"&eTime="+this.eTime);
				reqEntity.setContentType("application/x-www-form-urlencoded");
				postMethod.setEntity(reqEntity);
				
				HttpResponse responseP = client.execute(postMethod);
				String entityP = EntityUtils.toString(responseP.getEntity());
//				System.out.println(entityP);
				
				try{
					Document document = DocumentHelper.parseText(entityP);
					Element root = document.getRootElement();//获得根元素
					for (Iterator i = root.elementIterator("D"); i.hasNext();) {
			            Element foo = (Element) i.next();
//			            System.out.println(foo.getData());
			            if(null != foo.getData() && !"".equals(foo.getData())){
			            	GpsCarHis gpsCarHis = new GpsCarHis();
			            	String[] infoArray =  foo.getData().toString().split(";");
				            gpsCarHis.setCarid(infoArray[0]);
				            try{
				            	SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm:ss"); // 2012-12-02 04:38:06
					            Date cTime = format.parse(infoArray[1]);
					            gpsCarHis.setCtime(cTime);
				            } catch(ParseException e) {
				            	System.out.println("转换日期对象失败");
				            }
				            gpsCarHis.setNongps(infoArray[2]);
				            gpsCarHis.setClo(infoArray[3]);
				            gpsCarHis.setCla(infoArray[4]);
				            gpsCarHis.setCspeed(infoArray[5]);
				            gpsCarHis.setCdir(infoArray[6]);
				            gpsCarHis.setCstatus(infoArray[7]);
				            gpsCarHis.setCalarm(infoArray[8]);
				            gpsCarHis.setCfreealarm(infoArray[9]);
				            gpsCarHis.setCx(infoArray[10]);
				            gpsCarHis.setCy(infoArray[11]);
				            gpsCarHis.setConline(infoArray[12]);
				            
				            this.gpsCarHisList.add(gpsCarHis);
			            }
			        }
				} catch (DocumentException e){
					System.out.println("dom4j转换失败");
				}
				
			} catch(ClientProtocolException e) {
				System.out.println("http请求失败");
			} catch (IOException e) {
			}
		} else {
			System.out.println("登录失败");
		}
		return this.gpsCarHisList;
		
	}
	
}
